package com.zuokun.vo;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.zuokun.manager.RoomManager;
import lombok.Data;

import java.util.ArrayList;
import java.util.List;

@Data
public class RoomInfo {

    //房间id
    private Integer id;

    //房主id
    private Integer hostId;

    private Integer count = 0;

    private Boolean isPrepare;

    private static final Integer maxPlayerCount = 3;

    @JsonIgnore
    private List<PlayerInfo> playerList = new ArrayList<>();

    /**
     * 添加玩家
     *
     * @param playerInfo
     */
    public boolean addPlayer(PlayerInfo playerInfo) {
        if (playerList.size() >= maxPlayerCount) {
            return false;
        }
        playerList.add(playerInfo);
        playerInfo.setRoomId(this.id);
        //设置房主
        if (hostId == null) {
            hostId = playerInfo.getId();
        }
        return true;
    }

    /**
     * 移除玩家
     *
     * @param playerInfo
     * @return
     */
    public boolean removePlayer(PlayerInfo playerInfo) {
        playerList.remove(playerInfo);
        //房间没有玩家，则移除房间
        if(playerList == null || playerList.size() == 0){
            RoomManager.removeRoom(this.id);
        }else {
            //有玩家则设置新的房主
            setHostId(playerList.get(0).getId());
        }
        return true;
    }
}
